﻿ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SharpSoft.WPF.Controls
{

    public class CalligrapyStrokeRender : DynamicRenderer
    {

        public CalligrapyStrokeRender()
        {

        }
        private ImageSource imageSource;

        protected override void OnDrawingAttributesReplaced()
        {
            if (DesignerProperties.GetIsInDesignMode(this.Element))
                return;

            base.OnDrawingAttributesReplaced();

            var dv = new DrawingVisual();
            //  var size = 90;
            var scale = 0.4d;
            var img = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "Images\\pen.png", UriKind.Absolute));
            using (var conext = dv.RenderOpen())
            {
                //[关键]OpacityMask了解下？也许有童鞋想到的办法是，各种颜色的图片来一张？
                conext.PushOpacityMask(new ImageBrush(img));
                //用颜色生成画笔画一个矩形
                conext.DrawRectangle(new SolidColorBrush(this.DrawingAttributes.Color), null, new System.Windows.Rect(0, 0, img.Width * scale, img.Height * scale));
                conext.Close();
            }
            var rtb = new RenderTargetBitmap((int)(img.Width * scale), (int)(img.Height * scale), 96d, 96d, PixelFormats.Pbgra32);
            rtb.Render(dv);
            imageSource = BitmapFrame.Create(rtb);
            //[重要]此乃解决卡顿问题的关键！
            imageSource.Freeze();
        }

        //SharpSoft.Geometries.Kalman.KalmanFilter kw = new Geometries.Kalman.KalmanFilter();

        protected override void OnStylusMove(RawStylusInput rawStylusInput)
        {
            base.OnStylusMove(rawStylusInput);
            //var timestamp = rawStylusInput.Timestamp;
            //var ps = rawStylusInput.GetStylusPoints();

            //var l = ps.Clone();
            //l.Clear();// new StylusPointCollection(ps.Count);
            //for (int i = 0; i < ps.Count; i++)
            //{
            //    var pst = ps[i];
            //    var p = pst.ToPoint();
            //    var p2 = kw.Update(new Geometries.Point2D(p.X, p.Y), timestamp);
            //    pst.X = p2.X;
            //    pst.Y = p2.Y;
            //    l.Add(pst); 
            //} 
            //rawStylusInput.SetStylusPoints(l);
        }



        protected override void OnStylusMoveProcessed(object callbackData, bool targetVerified)
        {
            base.OnStylusMoveProcessed(callbackData, targetVerified);
        }

        protected override void OnDraw(DrawingContext dc, StylusPointCollection stylusPoints, Geometry geometry, Brush fillBrush)
        {
            foreach (var item in stylusPoints)
            {
                var p = item.ToPoint();
                dc.DrawEllipse(fillBrush, null, p, 20, 20);
            }
        }

        public struct StrokePoint
        {
            public double X { get; set; }

            public double Y { get; set; }
            /// <summary>
            /// 点的方向
            /// </summary>
            public double Angle { get; set; }
            /// <summary>
            /// 点的压力
            /// </summary>
            public double PressureFactor { get; set; }
        }
    }
}